

#include "u8g2.h"
#include "lcd.h"
#include "st7789v2.h"


#include <stdio.h>



#define W 40

#define H 30


uint16_t bitmapDma[2560];

uint8_t demo_bitmap[W*H*8];

void demo_bitmap_place_tile(uint8_t x, uint8_t y, uint8_t cnt, uint8_t arg_int, uint8_t *tile)

{
	//printf("demo_bitmap_place_tile x: %d y: %d cnt: %d\n", x, y, cnt);fflush(stdout);

  uint32_t i;

  for(i = 0; i < (8 * cnt * arg_int); i++ ){

	// printf("demo_bitmap_place_tile x: %d y: %d cnt: %d, x*def_tile_width*8+y*8*def_tile_height*W+i: %d tile[i]: 0x%x\n", x, y, cnt, x*def_tile_width*8+y*8*def_tile_height*W+i, tile[i]);fflush(stdout);

    demo_bitmap[x*8+ y*8*W+i] = tile[i];
	


	}

	// demo_bitmap_show();

}



void demo_bitmap_show(void)

{

  int x, y;

  for( y = 0; y < H*8; y++ )

  {

    for( x = 0; x < W*8; x++ )

    {

      if ( (demo_bitmap[x+(y/8)*W*8] & (1<<((y&7)))) != 0 )

      {

				//printf("*");

      }

      else

      {

				//printf(".");

      }

    }

    //printf("\n");

  }

}


void demo_bitmap_trans_bitmapDma(void){


 int x, y;
 int i=0;
 int num=0;

  for( y = 0; y < H*8; y++ )

  {
    for( x = 0; x < W*8; x++ )

    {

      if ( (demo_bitmap[x+(y/8)*W*8] & (1<<((y&7)))) != 0 )

      {
					
				
				bitmapDma[i]=LGRAYBLUE;
        i++;

      }
  
      else

      {

				bitmapDma[i]=BLACK;
        i++;

      }


    }
    if(y%8==7&&y!=0){

      LCD_Color_Fill_DMA(0,num,319,num+7,bitmapDma);
      num=num+8;
      i=0;


    }  

  }

}





void demo_bitmap_trans_lcd(void){



 int x, y;

  for( y = 0; y < H*8; y++ )

  {

    for( x = 0; x < W*8; x++ )

    {

      if ( (demo_bitmap[x+(y/8)*W*8] & (1<<((y&7)))) != 0 )

      {
					
				LCD_DrawPoint(x,y,BLUE);
				

      }

      else

      {

				LCD_DrawPoint(x,y,BLACK);

      }

    }


  }

}






static const u8x8_display_info_t u8x8_demo_display_info = 

{

  /* chip_enable_level = */ 0,

  /* chip_disable_level = */ 1,

  

  /* post_chip_enable_wait_ns = */ 20,

  /* pre_chip_disable_wait_ns = */ 10,

  /* reset_pulse_width_ms = */ 100, 	/* SSD1306: 3 us */

  /* post_reset_wait_ms = */ 100, /* far east OLEDs need much longer setup time */

  /* sda_setup_time_ns = */ 50,		/* SSD1306: 15ns, but cycle time is 100ns, so use 100/2 */

  /* sck_pulse_width_ns = */ 50,	/* SSD1306: 20ns, but cycle time is 100ns, so use 100/2, AVR: below 70: 8 MHz, >= 70 --> 4MHz clock */

  /* sck_clock_hz = */ 4000000UL,	/* since Arduino 1.6.0, the SPI bus speed in Hz. Should be  1000000000/sck_pulse_width_ns */

  /* spi_mode = */ 0,		/* active high, rising edge */

  /* i2c_bus_clock_100kHz = */ 4,

  /* data_setup_time_ns = */ 40,

  /* write_pulse_width_ns = */ 150,	/* SSD1306: cycle time is 300ns, so use 300/2 = 150 */

  /* tile_width = */ 40,

  /* tile_height = */ 30,

  /* default_x_offset = */ 2,

  /* flipmode_x_offset = */ 2,

  /* pixel_width = */ 320,

  /* pixel_height = */ 240

};





uint8_t u8x8_demo_d_stdio(U8X8_UNUSED u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)

{

	//printf("u8x8_demo_d_stdio msg: %d\n", msg);

  switch(msg)

  {

    case U8X8_MSG_DISPLAY_INIT:

      break;

    case U8X8_MSG_DISPLAY_SET_POWER_SAVE:

      if ( arg_int == 0 )

	      //demo_bitmap_trans_lcd();
        demo_bitmap_trans_bitmapDma();

      break;

    case U8X8_MSG_DISPLAY_SET_CONTRAST:

      break;

    case U8X8_MSG_DISPLAY_DRAW_TILE:

			//printf("u8x8_demo_d_stdio U8X8_MSG_DISPLAY_DRAW_TILE\n");

			// printf("U8X8_MSG_DISPLAY_DRAW_TILE x: %d y: %d cnt: %d arg_int: %d\n", ((u8x8_tile_t *)arg_ptr)->x_pos, ((u8x8_tile_t *)arg_ptr)->y_pos, ((u8x8_tile_t *)arg_ptr)->cnt, arg_int);

      demo_bitmap_place_tile(((u8x8_tile_t *)arg_ptr)->x_pos, ((u8x8_tile_t *)arg_ptr)->y_pos, ((u8x8_tile_t *)arg_ptr)->cnt, arg_int, ((u8x8_tile_t *)arg_ptr)->tile_ptr);

      break;

    case U8X8_MSG_DISPLAY_SETUP_MEMORY:

			//printf("u8x8_demo_d_stdio U8X8_MSG_DISPLAY_SETUP_MEMORY\n");

      u8x8_d_helper_display_setup_memory(u8x8, &u8x8_demo_display_info);

      break;

		case U8X8_MSG_DISPLAY_SET_FLIP_MODE:


		break;

    default:

      break;

  }

  return 1;

}




void u8g2_Setup_demo(u8g2_t *u8g2, const u8g2_cb_t *rotation, u8x8_msg_cb byte_cb, u8x8_msg_cb gpio_and_delay_cb)

{

  uint8_t tile_buf_height;

  uint8_t *buf;

	// printf("u8g2_SetupDisplay ...\n");fflush(stdout);

  u8g2_SetupDisplay(u8g2, u8x8_demo_d_stdio, u8x8_cad_001, byte_cb, gpio_and_delay_cb);

	// printf("u8g2_m_16_8_2 ...\n");fflush(stdout);

  buf = u8g2_m_48_30_f(&tile_buf_height);

	// printf("u8g2_SetupBuffer ...\n");fflush(stdout);

  u8g2_SetupBuffer(u8g2, buf, tile_buf_height, u8g2_ll_hvline_vertical_top_lsb, rotation);

}

uint8_t u8g2_Setup_demo_byte_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr){

    return 0;

}

uint8_t u8g2_Setup_demo_gpio_and_delay_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr){

    return 0;

}



#if 0

int main(int argc,const char **argv) {

	u8g2_t *u8g2 = malloc(sizeof(u8g2_t));



	//printf("u8g2_Setup_demo ...\n");fflush(stdout);

	u8g2_Setup_demo(u8g2, U8G2_R0, u8g2_Setup_demo_byte_cb, u8g2_Setup_demo_gpio_and_delay_cb);



	//printf("u8g2_InitDisplay ...\n");fflush(stdout);

	u8g2_InitDisplay(u8g2);

	//printf("u8g2_ClearBuffer ...\n");fflush(stdout);

	u8g2_ClearBuffer(u8g2);

	//printf("u8g2_SetPowerSave ...\n");fflush(stdout);

	u8g2_SetPowerSave(u8g2, 1);

	//printf("u8g2_SetFont ...\n");fflush(stdout);

	//u8g2_SetFont(u8g2, u8g2_font_ncenB08_tr);

	printf("u8g2_DrawStr hello world! ...\n");fflush(stdout);

		u8g2_SetFont(u8g2, u8g2_font_ncenB08_tr);
		
		u8g2_DrawStr(u8g2,10, 10, "Hello World!");

		u8g2_DrawStr(u8g2,10, 40, "Hello World!");



	u8g2_SendBuffer(u8g2);


	u8g2_SetPowerSave(u8g2, 0);

	return 0;

}

#endif
